package com.lsy.weizhi2.fragment;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.lsy.weizhi2.JsonObject.WoYueJson;
import com.lsy.weizhi2.R;
import com.lsy.weizhi2.adapter.ManagerAdapter;
import com.lsy.weizhi2.adapter.iAskAdapter;
import com.lsy.weizhi2.config.Constants;
import com.lsy.weizhi2.myutils.IAskAdapterCall;
import com.lsy.weizhi2.myutils.ManagerItem;
import com.lsy.weizhi2.utils.MyMethod;
import com.lsy.weizhi2.utils.MyOkHttpUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 嗨哟 on 2017/3/29.
 */
public class IAsk extends Fragment {
    private Context context;
    private RecyclerView recycle_view;
    private iAskAdapter iaskAdapter;
    private Gson gson;
    private List<WoYueJson.ListBean> list;
    private int pageNo;
    private boolean isload;
    private Handler handler=new Handler();
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.i_ask_fragment,container,false);
        recycle_view= (RecyclerView) view.findViewById(R.id.recycle_view);

        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        context=getActivity();
        gson=new Gson();
        list=new ArrayList<>();
        LinearLayoutManager manager=new LinearLayoutManager(context);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recycle_view.setLayoutManager(manager);
        iaskAdapter=new iAskAdapter(context,list,0);
        recycle_view.setAdapter(iaskAdapter);
    }

    @Override
    public void onResume() {
        super.onResume();
        pageNo=1;
        getData(1000);
        recycle_view.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                boolean bottom=recyclerView.canScrollVertically(-1);
                if(newState==RecyclerView.SCROLL_STATE_SETTLING&&!bottom&&!isload){
                    iaskAdapter.UpData(list, 1, 0);
                    isload=true;
                }else if(newState==RecyclerView.SCROLL_STATE_IDLE&&!bottom&&isload){

                    if(pageNo>=1&&list!=null&&list.size()>=10*pageNo){
                        iaskAdapter.UpData(list, 1, 1);
                        pageNo++;
                        getData(1000);
                    }else{
                        iaskAdapter.UpData(list, 1, 2);
                        handler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                iaskAdapter.UpData(list, 0, 0);
                            }
                        },500);
                    }

                }
            }
        });
        iaskAdapter.setiAskAdapterCall(new IAskAdapterCall() {
            @Override
            public void Cancel(int position) {
                CancelYueDan(list.get(position).getId()+"",position);
            }

            @Override
            public void Refuse(int position) {

            }

            @Override
            public void Accept(int position) {

            }
        });
    }

    private void CancelYueDan(String orderId, final int position){
        MyOkHttpUtils.getInstance(context).doPut(Constants.QuXiao+orderId+"/cancel",null)
                .excute(new MyOkHttpUtils.OKCallBack() {
                    @Override
                    public void onFailure(String message) {

                    }

                    @Override
                    public void onResponse(String message) {
                        Log.i("IAsk","message=取消=="+message);
                        if(message!=null){
                            try {
                                JSONObject jsonObject=new JSONObject(message);
                                boolean result=jsonObject.getBoolean("result");
                                if(result){
                                    getData(position);
                                }else{
                                    MyMethod.ShowDialog(context,jsonObject.getString("message"));
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

                    }
                });
    }

    private void getData(final int position){
        Map<String,String> map=new HashMap<>();
        map.put("pageSize","10");
        map.put("pageNo",pageNo+"");
        MyOkHttpUtils.getInstance(context).doGet(Constants.WoYue,map)
                .excute(new MyOkHttpUtils.OKCallBack() {
                    @Override
                    public void onFailure(String message) {

                    }

                    @Override
                    public void onResponse(String message) {
                        Log.i("IAsk","message==="+message);
                        if(message!=null){
                            try {
                                if(pageNo==1){
                                    list.clear();
                                }
                                JSONObject jsonObject=new JSONObject(message);
                                boolean result=jsonObject.getBoolean("result");
                                if(result){
                                    WoYueJson woYueJson=gson.fromJson(message,WoYueJson.class);
                                    list.addAll(woYueJson.getList());
                                    if(position==1000){
                                        iaskAdapter.UpData(list,0,0);
                                    }else{
                                        iaskAdapter.UpData_Position(list,0,0,position);
                                    }

                                }

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });
    }
}
